/**
 * 
 */
package com.jdy.haoduoai.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.intface.CommentDelegate;
import com.jdy.haoduoai.model.PainPointDiscussionItem;
import com.jdy.haoduoai.util.CircleTransform;
import com.jdy.haoduoai.util.Tools;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-11-29 下午3:07:21
 */
public class PainPointDiscussionAdapter extends BaseAdapter {
  private final Context mContext;

  private final LayoutInflater mInflater;

  private final SparseArray<View> mSpArray;

  private final ArrayList<PainPointDiscussionItem> mPainPointDiscussionList;

  private CommentDelegate mCommentDelegate;

  public void setCommentDelegate(CommentDelegate cd) {
    this.mCommentDelegate = cd;
  }

  public PainPointDiscussionAdapter(Context ctx,
      ArrayList<PainPointDiscussionItem> list) {

    this.mContext = ctx;

    this.mPainPointDiscussionList = list;

    this.mInflater = LayoutInflater.from(mContext);

    this.mSpArray = new SparseArray<View>();

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    if (mPainPointDiscussionList != null)
      return mPainPointDiscussionList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public PainPointDiscussionItem getItem(int position) {
    // TODO Auto-generated method stub
    if (mPainPointDiscussionList != null)
      return mPainPointDiscussionList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    convertView = mSpArray.get(position);
    if (convertView == null) {
      final PainPointDiscussionItem ppdi = mPainPointDiscussionList
          .get(position);
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.painpoint_discussion_item, null);
      holder.avatarImg = (ImageView) convertView
          .findViewById(R.id.dis_avatar_img);
      if (Tools.isNotEmpty(ppdi.member.avatar)) {
        Picasso
            .with(mContext)
            .load(ppdi.member.avatar)
            .placeholder(
                mContext.getResources().getDrawable(
                    R.drawable.default_discussion_avatar))
            .error(
                mContext.getResources().getDrawable(
                    R.drawable.default_discussion_avatar))
            .transform(new CircleTransform()).into(holder.avatarImg);
      }
      holder.nameTv = (TextView) convertView
          .findViewById(R.id.comment_user_name_tv);
      holder.nameTv.setText(ppdi.member.nickname);
      holder.timestampTv = (TextView) convertView
          .findViewById(R.id.timestamp_tv);
      holder.timestampTv.setText(ppdi.created_at);
      holder.contentTv = (TextView) convertView
          .findViewById(R.id.comment_content);
      holder.contentTv.setText(ppdi.comment);
      holder.replyImg = (ImageView) convertView.findViewById(R.id.reply_img);
      holder.replyImg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          if (mCommentDelegate != null)
            mCommentDelegate.publishCommentDelegate(ppdi.id);
        }
      });
      holder.numberTv = (TextView) convertView.findViewById(R.id.like_num_tv);
      holder.numberTv.setText(String.valueOf(ppdi.like));

      holder.likeImg = (ImageView) convertView
          .findViewById(R.id.like_comment_img);

      if (ppdi.if_like) {
        holder.likeImg.setImageResource(R.drawable.like_yellow);
      } else {
        holder.likeImg.setImageResource(R.drawable.like_icon);
      }

      holder.likeImg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          if (mCommentDelegate != null)
            mCommentDelegate.likeDelegate(ppdi.id);
        }
      });
      holder.replyLv = (com.jdy.haoduoai.view.ListViewForScrollView) convertView
          .findViewById(R.id.comment_lv);
      holder.replyLayout = (RelativeLayout) convertView
          .findViewById(R.id.reply_list_layout);

      if (Tools.isNotEmpty(ppdi.children_comments)
          && ppdi.children_comments.size() != 0) {

        DiscussionAdapter adapter = new DiscussionAdapter(mContext,
            ppdi.children_comments, ppdi.member);

        adapter.setCommentDelegate(mCommentDelegate);

        holder.replyLv.setAdapter(adapter);

      } else {
        holder.replyLayout.setVisibility(View.GONE);
      }

      Tools.setListViewHeightBasedOnChildren(holder.replyLv);

      // RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
      // ViewGroup.LayoutParams.MATCH_PARENT,
      // ViewGroup.LayoutParams.MATCH_PARENT);
      // params.setMargins(0, 0, 0, Tools.getPixelByDip(mContext, 20)); //
      // left,top,right,bottom
      // holder.replyLv.setLayoutParams(params);

      convertView.setTag(holder);
      mSpArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
  }

  private class ViewHolder {
    ImageView avatarImg;

    TextView nameTv;

    TextView timestampTv;

    ImageView replyImg;

    TextView numberTv;

    ImageView likeImg;

    TextView contentTv;

    RelativeLayout replyLayout;

    com.jdy.haoduoai.view.ListViewForScrollView replyLv;
  }

}
